package com.libmessagecenter

import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import com.libbase.utils.JsonUtils
import com.libbase.utils.ThreadUtils
import com.libmessagecenter.models.Message
import com.xiaomi.mipush.sdk.ErrorCode
import com.xiaomi.mipush.sdk.MiPushClient
import com.xiaomi.mipush.sdk.MiPushCommandMessage
import com.xiaomi.mipush.sdk.MiPushMessage
import com.xiaomi.mipush.sdk.PushMessageHelper
import com.xiaomi.mipush.sdk.PushMessageReceiver


class MiPushMessageReceiver : PushMessageReceiver() {
    companion object {
        const val TAG = "MiPushMessageReceiver"

        val pushSDK = object : PushSDKInterface {
            override fun register(context: Context, appId: String?, appKey: String?) {
                MiPushClient.registerPush(context, appId, appKey)
            }

            override fun handleIntent(intent: Intent): Boolean {
                val miPushMessage: MiPushMessage? =
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                        intent.getSerializableExtra(
                            PushMessageHelper.KEY_MESSAGE,
                            MiPushMessage::class.java
                        )
                    } else {
                        @Suppress("DEPRECATION")
                        intent.getSerializableExtra(PushMessageHelper.KEY_MESSAGE) as? MiPushMessage
                    }

                return miPushMessage?.let {
                    val message: Message = JsonUtils.json.fromJson(it.content, Message::class.java)
                    LibMessageCenter.instance.onMessageDidReceive(message)

                    true
                } ?: false
            }
        }
    }

    override fun onNotificationMessageClicked(context: Context?, message: MiPushMessage) {
        Log.i(TAG, "onNotificationMessageClicked, $message")
    }

    override fun onNotificationMessageArrived(context: Context?, miPushMessage: MiPushMessage) {
        if (!miPushMessage.isArrivedMessage) {
            val message = JsonUtils.json.fromJson(miPushMessage.content, Message::class.java)

            ThreadUtils.instance.executeInMainThread {
                LibMessageCenter.instance.onMessageWillPresent(message)
            }
        }
    }

    override fun onCommandResult(context: Context?, message: MiPushCommandMessage) {
        Log.v(TAG, "onCommandResult is called. $message")
    }

    override fun onReceiveRegisterResult(context: Context?, message: MiPushCommandMessage) {
        Log.v(TAG, "onReceiveRegisterResult is called. $message")

        if (MiPushClient.COMMAND_REGISTER != message.command
            || message.resultCode != ErrorCode.SUCCESS.toLong()
        ) {
            return
        }

        message.commandArguments?.first()?.let { deviceToken ->
            ThreadUtils.instance.executeInMainThread {
                LibMessageCenter.instance.register(deviceToken)
            }
        }
    }
}